<?php 
include_once("../tools/DBInterface.class.php");
include_once("Record.class.php");
include_once("../Player/Player.class.php");
include_once("../lib/Smarty/Smarty.class.php");

$smarty = new Smarty();
$player = new Player();
$record = new Record();
$dbinterface = new DBInterface();

switch($_GET['action'])
{
    case 1: //When te section Record is called from the menu
        $smarty->assign("tourney",$dbinterface->exec_select(array('*'),'tourney'));
        $smarty->assign("records",$record->getList());
        $smarty->display("Record/records_main.tpl");
    break;
    
    case 2://When the user press the tab Record
        $smarty->assign("tourney",$dbinterface->exec_select(array('*'),'tourney'));
        $smarty->assign("records",$record->getList());
        $smarty->display("Record/records_main_cont.tpl");
    break;
    
    case 3://When the user press the tab add Record old
        $smarty->assign("allplayers",$dbinterface->exec_select(array('pla_id', '(pla_first_name || pla_last_name) as name', 'pla_gender', 'pla_del_id'),'player'));
        $smarty->assign("tourney",$dbinterface->exec_select(array('*'),'tourney'));
        $smarty->display("Record/records_new.tpl");
    break;
    
    case 4: //To create a new record
        $values = explode("_",$_POST['rec_pla_id']);
         $_POST['rec_pla_id'] = $values[0];
        if($record->create($_POST)==false)
        {
            echo '<script type="text/javascript">
                    alert("Ha ocurrido un error con el record, intentalo de nuevo");
                </script>';
        }
        else
        {
            echo '<script type="text/javascript">
                    alert("Record agregado exitosamente");
                </script>';
        }
        $smarty->display("Record/records_new.tpl");
    break;
    
    case 5: //To create a new player extern
        if($record->create($_POST)==false)
        {
            echo '<script type="text/javascript">
                    alert("Ha ocurrido un error con el jugador, intentalo de nuevo");
                </script>';
        }
        else
        {
            echo '<script type="text/javascript">
                    alert("Jugador externo agregado exitosamente");
                </script>';
        }
        $smarty->display("Record/records_new.tpl");
    break;
    
    case 6: //To visulize the info of a record
//         echo '<script type="text/javascript">
//                     alert("entra");
//                 </script>';
        $smarty->assign("onerecord",$record->getData($_GET['id']));
        $smarty->display("Record/records_info.tpl");
    break;
    
    case 7: //To delete a record
        $record->remove($_GET['id']);
        echo '<script type="text/javascript">
                updater(\'list\',\'Record/WP_Record.php?action=2\');
            </script>';
    break;
    
    case 8: //Find player all players
        $smarty->assign("allplayers",$dbinterface->exec_select(array('pla_id', '(pla_first_name || pla_last_name)', 'pla_gender', 'pla_del_id'),'player'));
    break;
}
?>